package com.benmei.weike.web;

import com.benmei.weike.common.Constants;
import com.benmei.weike.common.Lang;
import com.benmei.weike.dto.MemberRegisterByCode;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.service.SMSService;
import com.benmei.weike.service.StudentAccountService;
import com.benmei.weike.service.common.MemcachedService;
import com.benmei.weike.util.JsonUtil;
import com.benmei.weike.util.Sms;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Peter on 2018/11/10.
 * 学生账户2
 */
@RestController
public class StudentAccountController {
    public static final Logger logger = LoggerFactory.getLogger(WeikeStudentController.class);
    //log 记录方法入惨的前缀
    private static final String prefix_in = "\nparameters in -->> ";
    //log 记录方法出惨的前缀
    private static final String prefix_out = "\nparameters out -->>";

    @Autowired
    private StudentAccountService studentAccountService;
    @Autowired
    private SMSService smsService;

    // 通过注册码注册
    @RequestMapping(value = "/v4.2/student/member/register/bycode", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    @CrossOrigin
    public RetInfo registerBycode(@RequestParam(value = "memb_phone", required = true) String memb_phone,
                                  @RequestParam(value = "invite_code", required = true) String invite_code,
                                  @RequestParam(value = "memb_phone_area", required = true) String memb_phone_area) {
        try {
            //打印入参
            logger.info(Constants.prefix_out + "memb_phone:" + memb_phone + ",invite_code:" + invite_code + ",memb_phone_area:" + memb_phone_area);
            MemberRegisterByCode memberRegisterByCode = new MemberRegisterByCode();
            memberRegisterByCode.setInvite_code(invite_code);
            memberRegisterByCode.setMemb_phone(memb_phone);
            memberRegisterByCode.setMemb_phone_area(memb_phone_area);
            RetInfo retInfo = studentAccountService.registerByCode(memberRegisterByCode);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    // 发送短信验证码
    @RequestMapping(value = "/v5.0/student/account/sendSmsCode/{phone}", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json; charset=utf-8")
    @ResponseBody
    public RetInfo sendSmsCode(@PathVariable("phone") String phone) {
        try {
            //打印入参
            logger.info(Constants.prefix_out + JsonUtil.toJson(phone));

            RetInfo retInfo = smsService.sendMessage(new Sms(phone));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    // 通过手机号和验证码登陆或注册
    @RequestMapping(value = "/v5.0/student/account/registerOrLoginByPhoneAndCode", method = {RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    @CrossOrigin
    public RetInfo registerOrLoginByPhoneAndCode(@RequestParam(value = "memb_phone", required = true) String memb_phone,
                                                 @RequestParam(value = "code", required = true) Integer code) {
        try {
            //打印入参
            logger.info(Constants.prefix_out + "memb_phone:" + memb_phone + ",code:" + code);
            if (StringUtils.isBlank(memb_phone)) {
                throw new ClientException("无效的手机号");
            }
            if (code == null) {
                throw new ClientException("验证码无效");
            }

            RetInfo retInfo = studentAccountService.registerOrLoginByPhoneAndCode(memb_phone, code);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }



}
